package cn.wit.sst.ml.kmeans;

import java.util.Arrays;

/**
 * 代表一个的样本点
 *
 */
public class Sample implements Comparable<Sample> {
	/** 样本点属性值 */
	private double[] attrs;

	public Sample(double[] attrs) {
		this.attrs = attrs;
	}

	public double[] getAttrs() {
		return attrs;
	}

	public int size() {
		return attrs.length;
	}

	@Override
	public String toString() {
		return Arrays.toString(attrs);
	}

	@Override
	public int compareTo(Sample o) {
		if (o == null) return 1;

		if (this.attrs == null || o.attrs == null) {
			return this.attrs == null ? -1 : 1;
		}

		if (this.attrs.length != o.attrs.length) {
			throw new RuntimeException("样本数据不一致！");
		}

		double sum = 0;
		for (int i = 0; i < this.attrs.length; i++) {
			sum += Math.pow(this.attrs[i], 2) - Math.pow(o.attrs[i], 2);
		}

		return sum > 0 ? 1 : -1;
	}
}